package com.example.module_filemanage.MVVM.View.Dialog;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import com.example.module_filemanage.Interface.ICallBack;
import com.example.module_filemanage.Interface.IUpLoadFile;
import com.example.module_filemanage.MVVM.Module.Entil.Item_FileInfo_module;
import com.example.module_filemanage.MVVM.Module.QiNiu.FileManager2;
import com.example.module_filemanage.Public.Data;
import com.example.module_filemanage.R;
import com.example.module_filemanage.Utils.Tool;

import java.util.UUID;

import static android.content.ContentValues.TAG;

/**
 * Created by SJ on 2018/4/28.
 */

public class MDialog {
    private Activity mContext;
    private static volatile MDialog mInstance;

    public MDialog(Activity IContext) {
        mContext=IContext;
    }

    public static MDialog getInstance(Activity IContext) {
        if (mInstance == null) {
            synchronized (MDialog.class) {
                if (mInstance == null) {
                    mInstance=new MDialog(IContext);
                }
            }
        }
        return mInstance;
    }
    public  void delFile(final Item_FileInfo_module item, final ICallBack callback){

        new AlertDialog.Builder(mContext)
                .setIcon(R.drawable.ic_help_black_24dp)
                .setTitle("删除文件")
                .setMessage("确定要删除【"+item.filename+"】吗？")
                .setCancelable(true)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        FileManager2.getInstance().delFile( item.getSpacename(), item.getFilename(), callback);

                    }
                })
                .setNegativeButton("取消", new DialogInterface.
                        OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .show();
    }
    public void downloadFile(final Item_FileInfo_module item){
        new AlertDialog.Builder(mContext)
                .setIcon(R.drawable.ic_help_black_24dp)
                .setTitle("复制文件")
                .setMessage("复制【"+item.filename+"】的链接")
                .setCancelable(true)
                .setPositiveButton("复制链接", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String ss=item.getFileurl()+"?attname=&v="+ UUID.randomUUID().toString().replace("-","");
                        ClipboardManager c = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                        // 将文本内容放到系统剪贴板里。
                        c.setText(ss);
                        Toast.makeText(mContext, "复制文件下载链接成功！", Toast.LENGTH_SHORT).show();

                    }
                })
                .setNegativeButton("取消", new DialogInterface.
                        OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .show();
    }
    public void rename(final Item_FileInfo_module item, final ICallBack back){
        final EditText input=new EditText(mContext);
            new AlertDialog.Builder(mContext)
                            .setTitle("请输入新的文件名")
                            .setView(input)
                            .setPositiveButton("确定",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,
                                                int which) {
                                            final String value = input.getText().toString ();
                                            final String ss=item.getFilename().substring(item.getFilename().lastIndexOf("."));
                                            FileManager2.getInstance().rename(item.getSpacename(), item.getFilename(), value+"-"+Data.account+ss, new ICallBack<Bundle>() {
                                                @Override
                                                public void success(Bundle items) {
                                                    if (items.getInt("code")==200) {
                                                        Toast.makeText(mContext, "重命名成功！", Toast.LENGTH_SHORT).show();
                                                        back.success(value+"-"+Data.account+ss);
                                                    }else {
                                                        Toast.makeText(mContext, "失败！", Toast.LENGTH_SHORT).show();
                                                    }
                                                }
                                            });
                                        }
                                     })

            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface IDialogInterface, int II) {

                }
            })  //设置取消按 钮并且绑定监听事件
            .create()//设置取消按钮并创建
            .show();//显示

    }
    //上传文件/图片
    static String path;
    static String spacename;
    /**上传文件*/
    public void upLoadFile(final String filepath,String _spacename) {
        path = filepath;
        spacename=_spacename;
        final String ss = path.substring(path.lastIndexOf("."));
        if (path.matches(".+\\.(jpg|jpeg|png|gif|JPG|JPEG|PNG)")) {
            new AlertDialog.Builder(mContext)
                    .setIcon(R.drawable.ic_help_black_24dp)
                    .setTitle("图片确认")
                    .setMessage("是否压缩图片")
                    .setPositiveButton("压缩", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(mContext, "压缩中……", Toast.LENGTH_SHORT).show();
                            Tool.CompressImage(mContext.getApplicationContext(), filepath, new ICallBack<String>() {
                                @Override
                                public void success(String items) {
                                    path = items;
                                    upLoadFile2(path,ss);
                                }
                            });
                        }
                    })
                    .setNegativeButton("不压缩", new DialogInterface.
                            OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            upLoadFile2(path);
                        }
                    })
                    .show();

        }
    }
    public void upLoadFile2(final String path) {
        upLoadFile2(path,null);
    }
    public void upLoadFile2(final String path,String _end){
        String ss =_end!=null?_end: path.substring(path.lastIndexOf("."));
        final ProgressDialog progressDialog = new ProgressDialog(mContext);
        progressDialog.setProgress(0);
        progressDialog.setTitle("上传进度");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(100);

        final EditText input=new EditText(mContext);

        String name=_end==null?path.substring(path.lastIndexOf("/")+1,path.lastIndexOf(".")):path.substring(path.lastIndexOf("/")+1);
        input.setText(name+"-"+ Data.account+ss);
        new AlertDialog.Builder(mContext)
                .setTitle("请输入文件名")
                .setView(input)
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                String value = input.getText().toString ();
                                progressDialog.show();
                                Log.e(TAG, "uploadFile: 5");
                                FileManager2.getInstance().uploadFile(path, value, spacename, new IUpLoadFile() {
                                    @Override
                                    public void complete() {
                                       mContext.runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(mContext, "文件上传完成！", Toast.LENGTH_SHORT).show();
                                                progressDialog.cancel();
                                            }
                                        });

                                    }

                                    @Override
                                    public void faile(final String error) {

                                        mContext.runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                Toast.makeText(mContext, error, Toast.LENGTH_SHORT).show();
                                            }
                                        });
                                        progressDialog.cancel();
                                    }

                                    @Override
                                    public void progress(double progress) {
                                        Log.e(TAG, "progress: "+String.valueOf(progress));
                                        progressDialog.setProgress((int)(progress*100.0f));

                                    }
                                });
                            }
                        })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })  //设置取消按 钮并且绑定监听事件
                .create()//设置取消按钮并创建
                .show();//显示


    }

}
